# coding=utf-8
# Time : 2020/10/19 14:10
# Author : huxin
from flask_apscheduler import json

from common.DateTimeUtil import *
from common.YmlUtil import YmlUtil
from RootPath import RootPath
import os
import random
import time


class CumulativeFlow:
    """
    计算累计流量
    """
    path = os.path.join(RootPath.getRootPath(), "resources/CumulativeData.yml")
    key = "Flow"
    para = "CumulativeFlow"

    def judgeTime(self, startTime,endTime):
        now_time = int(time.strftime("%H%M%S"))
        if startTime < now_time < endTime:
            return True
        else:
            return False

    def sumFlow(self):

        flow_vale = YmlUtil.readYml(path=self.path, key=self.key, para=self.para)
        global zengliang
        zengliang = round(random.uniform(2, 3), 2)

        #根据时间段进行判断：
        times = ((70000,90000),(110000,130000),(173000,200000))
        for i in times:
            if self.judgeTime(i[0], i[1]):
                zengliang = round(random.uniform(3, 3.5), 2)
                break
            else:
                continue
        #print("增量值是：" + str(zengliang))
        flow_vale = float(flow_vale) + zengliang
        flow_vale = round(flow_vale, 2)

        #将数据回写
        result = YmlUtil.writeYml(path=self.path, key=self.key, para=self.para, value=flow_vale)
        #print(result)

    def readFlow(self):

        flow_vale = YmlUtil.readYml(path=self.path, key=self.key)
        flow_vale["CumulativeFlow"]=json.dumps(flow_vale["CumulativeFlow"])
        return flow_vale

# value  = CumulativeFlow().readFlow()
# # print(value)